# Calendar/DateTIme Select Dialog
#
# Copyright (C) 2010 Yotam Medini <yotam.medini@gmail.com>
#
# This file is part of Gupicasa.
#
# Gupicasa is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Gupicasa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gupicasa.  If not, see <http://www.gnu.org/licenses/>.


import gtk
import datetime

# local
import debug
import ugtk
import uicommon


_dt_maker =  datetime.datetime(datetime.MINYEAR, 1, 1, 0, 0, 0)


def datetime_now(utc=True):
    global _dt_maker
    dt = _dt_maker.utcnow() if utc else _dt_maker.now()
    return dt


class SelectDateTimeDialog(gtk.Dialog):


    def __init__(self, title="Select Date/Time", topwin=None, utc=False,
                 value=None, common=None):
        if utc:
            title += " (UTC)"
        if common is None:
            common = uicommon.Common()
        flags = gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
        gtk.Dialog.__init__(self, title=title, parent=topwin, flags=flags,
                            buttons=(gtk.STOCK_OK, gtk.RESPONSE_OK,
                                     gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL))
        self.set_icon(common.icons_pixbuf["gupicasa"])
        self.utc = utc
        self.init_value = value
        stock = gtk.image_new_from_stock(gtk.STOCK_DIALOG_QUESTION,
                                         gtk.ICON_SIZE_DIALOG)
        hb_main = gtk.HBox()
        hb_main.pack_start(stock, False, False, 0)
        vbox = gtk.VBox()

        self.cal = gtk.Calendar()
        vbox.pack_start(self.cal)

        hms = gtk.HBox()
        self.hh = ugtk.IntegerSpinButton(0, 23)
        self.mm = ugtk.IntegerSpinButton(0, 59)
        self.ss = ugtk.IntegerSpinButton(0, 59)
        hms.pack_start(self.hh, False, False, 1)
        hms.pack_start(gtk.Label(":"), False, False, 1)
        hms.pack_start(self.mm, False, False, 1)
        hms.pack_start(gtk.Label(":"), False, False, 1)
        hms.pack_start(self.ss, False, False, 1)

        hb = gtk.HBox()
        hb.pack_start(gtk.Label("Time:"), False, False, 2)
        hb.pack_start(hms, False, False, 2)
        bnow = ugtk.StockButton(gtk.STOCK_JUMP_TO)
        bnow.connect('clicked', self.now, None)
        hb.pack_start(bnow, False, False, 2)
        if value is not None:
            self.refresh()
            brefresh = ugtk.StockButton(gtk.STOCK_REFRESH)
            brefresh.connect('clicked', self.refresh, None)
            hb.pack_start(brefresh, False, False, 2)
        vbox.pack_start(hb, padding=2)

        hb_main.pack_start(vbox)
        hb_main.show_all()
        self.vbox.pack_start(hb_main)


    def now(self, w, dummy):
        self.set_datetime(datetime_now())


    def refresh(self, w=None, dummy=None):
        self.set_datetime(self.init_value)


    def set_datetime(self, dt):
        self.cal.clear_marks()
        self.cal.select_month(dt.month - 1, dt.year)
        self.cal.select_day(dt.day)
        self.hh.set_value(dt.hour)
        self.mm.set_value(dt.minute)
        self.ss.set_value(dt.second)


    def get_datetime(self):
        ymd = self.cal.get_date()
        hms = map(lambda e: e.iget_value(), [self.hh, self.mm, self.ss])
        ymdhms = ([ymd[0], ymd[1] + 1, ymd[2]] + hms) # Bump month index
        debug.log("ymdhms=%s" % str(ymdhms))
        dt = datetime.datetime(*ymdhms)
        return dt


if __name__ == '__main__':

    import sys


    def select_time_cb(w, topwin):
        obama = datetime.datetime(2008, 11, 5, 6, 0, 1)
        d = SelectDateTimeDialog(topwin=topwin, utc=True, value=obama)
        response = d.run()
        sys.stdout.write("response=%d\n" % response)
        if response == gtk.RESPONSE_OK:
            dt = d.get_datetime()
            sys.stdout.write("DateTime: %s\n" % dt)
        else:
            sys.stdout.write("SelectDateTimeDialog ignored\n")
        d.destroy()


    topwin = gtk.Window()
    topwin.set_title("Calendar-Demo")
    topwin.connect('delete-event', gtk.main_quit)
    vbox = gtk.VBox()
    b = gtk.Button("Pick Date/Time...")
    b.connect('clicked', select_time_cb, topwin)
    vbox.pack_start(b)
    topwin.add(vbox)
    topwin.show_all()
    gtk.main()
    sys.stderr.write("Exiting\n")
    sys.exit(0)

